package demo3;

import java.util.Arrays;

class Student implements Comparable<Student>{
    private String name;
    private int age;
    private float score;

    public Student(String name, int age,float score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return -(int)(this.score - o.score);
    }
}

public class Test {
    //通过冒泡排序 来 实现 Arrays.sort()
    public static void sort (Comparable[] comparables) {
        for (int i = 1; i < comparables.length; i++) {
            for (int j = 0; j < comparables.length - i; j++) {
                if (comparables[j].compareTo(comparables[j + 1]) < 0) {
                    Comparable temp = comparables[j];
                    comparables[j] = comparables[j + 1];
                    comparables[j + 1] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("张三",21,98);
        students[1] = new Student("张三",21,199);
        students[2] = new Student("张三",21,12);
        /*Arrays.sort(students);
        System.out.println(Arrays.toString(students));*/
        sort(students);
        System.out.println(Arrays.toString(students));
    }
}
